﻿#!/usr/bin/env python3

def baby(cls):
    '''
    不带参的decorator
    '''
    class BabyCls():
        def __init__(self):
            self.inner = cls()
        def hello(self):
            return self.inner.hello() + ", baby."
    return BabyCls

@baby
class Hello:
    def hello(self):
        return "hello"


def baby2(name):
    '''
    带参的decorator
    '''
    def baby(cls):
        class BabyCls():
            def __init__(self):
                self.inner = cls()
            def hello(self):
                return self.inner.hello() + "," + name
        return BabyCls
    return baby

@baby2('Tom')
class Hello2:
    def hello(self):
        return "hello"




class baby3():
    def __init__(self, name):
        self.name = name
    def __call__(self, cls):
        class Wrapper():
            name = ''
            def __init__(self):
                self.inner = cls()
            def hello(self):
                return self.inner.hello() + "," + Wrapper.name
        Wrapper.name = self.name
        return Wrapper

@baby3('Tom')
class Hello3:
    def hello(self):
        return "hello"

def main():
    '''
class decorator -
修饰类的decorator.
decorator使用类实现的话,要求该类应该实现__init__()和__call__().
若是带参的,可以在__init__()中传入

    '''
    print(main.__doc__)
    h = Hello()
    print(h.hello())

    h = Hello2()
    print(h.hello())

    h = Hello3()
    print(h.hello())


if __name__=='__main__':
    main()
